import React, { useState, useEffect } from 'react';
import styles from './showuse.less';
import { Button } from 'antd';
import { Table } from 'antd';
import {  useSelector, userModelIstate } from 'umi'
let Showuser: React.FC = function () {
  const { identitylist, userallinfo, apilist, viewlist, apiidentity, viewidentity } = useSelector((module: { usermanage: userModelIstate }) => module.usermanage)
  let [toparr, settoparr] = useState(["用户数据", "身份数据", 'api接口权限', "身份和api接口关系", "视图接口权限", '身份和视图权限关系'])
  const [title, settitle] = useState("用户数据")
  let a = [{
    title: '用户名',
    dataIndex: 'name',
  },
  {
    title: '密码',
    dataIndex: 'pwd',
  },
  {
    title: '身份',
    dataIndex: 'shenfeng',
  },
  ];
  let [columns, setcolumns] = useState<any>([])
  let [defaultindex, setdefaultindex] = useState(0)
  let [data, setdata] = useState<any>()

  useEffect(() => {
    let b: any = []
    userallinfo.map(item => {
      b.push({
        name: item.user_name,
        pwd: item.user_pwd,
        shenfeng: item.identity_text,
        key: item.user_name
      })
    })
    setdata(b)
    setcolumns(a)
  }, [])



  let tab = (item: string, index: number) => {
    setdefaultindex(index)
    settitle(item)
    if (item === "用户数据") {
      let a = [{
        title: '用户名',
        dataIndex: 'name',
      },
      {
        title: '密码',
        dataIndex: 'pwd',
      },
      {
        title: '身份',
        dataIndex: 'shenfeng',
      },
      ];
      setcolumns(a)
      let b: any = []
      userallinfo.map(item => {
        b.push({
          name: item.user_name,
          pwd: item.user_pwd,
          shenfeng: item.identity_text,
          key: item.user_name
        })
      })
      setdata(b)
    }
    if (item === "身份数据") {
      let a = [{
        title: '身份id',
        dataIndex: 'id',
      },
      {
        title: '身份',
        dataIndex: 'shenfen',
      },
      ];
      setcolumns(a)
      let b: any = []
      identitylist.map(item => {
        b.push({
          id: item.identity_id,
          shenfen: item.identity_text,
        })
      })
      setdata(b)
    }
    if (item === "api接口权限") {
      let a = [{
        title: '接口权限id',
        dataIndex: 'id',
      },
      {
        title: '接口权限名',
        dataIndex: 'name',
      },
      {
        title: '接口权限url',
        dataIndex: 'url',
      },
      {
        title: '方式',
        dataIndex: 'method',
      },
      ];
      setcolumns(a)
      let b: any = []
      apilist.map(item => {
        b.push({
          id: item.api_authority_id,
          name: item.api_authority_text,
          url: item.api_authority_url,
          method: item.api_authority_method
        })
      })
      setdata(b)
    }
    if (item === "身份和api接口关系") {
      let a = [{
        title: '身份',
        dataIndex: 'identity',
      },
      {
        title: '身份id',
        dataIndex: 'identityid',
      },
      {
        title: 'api名',
        dataIndex: 'name',
      },
      {
        title: 'api地址',
        dataIndex: 'url',
      },
      {
        title: '请求方式',
        dataIndex: 'method',
      },
      ];
      setcolumns(a)
      let b: any = []
      apiidentity.map(item => {
        b.push({
          identity: item.identity_text,
          name: item.api_authority_text,
          url: item.api_authority_url,
          method: item.api_authority_method,
          identityid: item.identity_api_authority_relation_id
        })
      })
      setdata(b)
    }
    if (item === "视图接口权限") {
      let a = [{
        title: '视图权限id',
        dataIndex: 'viewauthorityid',
      },
      {
        title: '视图',
        dataIndex: 'name',
      },
      {
        title: '视图id',
        dataIndex: 'viewid',
      }
      ];
      setcolumns(a)
      let b: any = []
      viewlist.map(item => {
        b.push({
          viewid: item.view_id,
          name: item.view_authority_text,
          viewauthorityid: item.view_authority_id,
        })
      })
      setdata(b)
    }
    if (item === "身份和视图权限关系") {
      let a = [{
        title: '身份视图id',
        dataIndex: 'identityviewid',
      },
      {
        title: '身份',
        dataIndex: 'name',
      },
      {
        title: '视图',
        dataIndex: 'view',
      },
      {
        title: '视图id',
        dataIndex: 'viewid',
      },
      ];
      setcolumns(a)
      let b: any = []
      viewidentity.map(item => {
        b.push({
          identityviewid: item.identity_view_authority_relation_id,
          name: item.identity_text,
          view: item.view_authority_text,
          viewid: item.view_id
        })
      })
      setdata(b)
    }
  }

  return (
    <div className={styles.show}>
      <span className={styles.title}>用户展示</span>
      <div className={styles.contentlist}>
        <div className={styles.top}>
          {toparr.map((item, index) => {
            return <Button size="large" key={index} onClick={() => tab(item, index)} type={index == defaultindex ? "primary" : 'default'}>{item}</Button>
          })}
        </div>
        <span className={styles.sub}>{title}</span>
        {columns && data && <Table columns={columns} dataSource={data} />}
      </div>
    </div>
  );
}
export default Showuser